﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using FTchina;

public partial class UI_preset : System.Web.UI.Page
{

    string RestRoomSql = "update {1} set State=0,StateDate='{2}',CustomerID='' where id in ({0})";
    string RestDinnerSql = "update {1} set State=0,StateDate='{2}',CustomerID='' where id in ({0})";

    string OrderRomm = " update {1} set State=1,StateDate='{2}',CustomerID='009' where id in ({0})";//到店客人入住，非电话客人

    string OrderDinner = "update {1} set xxxxxxCustomerID='009' ,xxxxxxDate='{2}' , xxxxxxcount='{3}' where id in ({0})";//到店用餐早餐
    string RestDinner = "update {1} set xxxxxxCustomerID='' ,xxxxxxDate='{2}' where id in ({0})";//清空早餐状态

    string AlertMessage = "编号{0}的项操作{1}";//操作结果提示信息

    protected void Page_Load(object sender, EventArgs e)
    {
        FTchina.basic.CheckAdmin(this);
        FTchina.basic.ChkPoewr(this, FTchina.Api.htmlpath.GetCurrentPagePath());

        string ids = FTchina.basic.SafeRequest(Request["ids"]);
        string op = FTchina.basic.SafeRequest(Request["op"]);
        string type = FTchina.basic.SafeRequest(Request["type"]);

        if (ids.Length == 0) { GoBack(); }
        if (op.Length == 0) { GoBack(); }

        MainDo(ids, op,type);
        GoBack();
    }

    /// <summary>
    /// 返回上一页
    /// </summary>
    protected void GoBack()
    {
        string back = FTchina.basic.SafeRequest(Request["back"]);
        if (back.Length > 0)
        {
            JS.JavaScriptLocationHref(back);
        }
        else
        {
            JS.GoHistory(-1);
        }
    }
    protected void MainDo(string ids, string op,string type)
    {
        ArrayList al = new ArrayList();//操作结果的提示集合
        if (ids.Contains(","))
        {
            string[] idsList = ids.Split(',');
            foreach (string str in idsList)
            {
                if (str.Trim().Length > 0)
                {
                    al.Add(ExecList(str, op,type));
                }
            }
        }
        else
        {
            al.Add(ExecList(ids, op,type));
        }


        //将操作结果alert出来
        string tmp = string.Empty;
        foreach (string str in al)
        {
            tmp += str + "\\n";//弹出信息换行
        }
        //  tmp = tmp.Substring(0, tmp.Length - 2);
        // LogNet.LogBLL.debug(tmp);
        JS.Alert(tmp);
    }

    /// <summary>
    /// 执行结果的集合
    /// </summary>
    /// <param name="ids"></param>
    /// <param name="tb"></param>
    /// <param name="op"></param>
    /// <returns></returns>
    protected String ExecList(string ids, string op,string type)
    {
        string tmp = string.Empty;


        if (ExecOpSqlSingleByID(ids, op, type))
        {
            tmp = string.Format(AlertMessage, ids, "成功！");
        }
        else
        {
            tmp = string.Format(AlertMessage, ids, "失败！");
        }
        return tmp;
    }


    /// <summary>
    /// 执行单个操作命令
    /// </summary>
    /// <param name="ids"></param>
    /// <param name="tb"></param>
    /// <param name="op"></param>
    /// <returns></returns>
    protected Boolean ExecOpSqlSingleByID(string ids, string op,string type)
    {
        if (type == "order")
        {//预定的时候插入一个预定记录
            InsertOrderList(ids,op);
        }
        return ExecSqls(MakeSqls(ids, op,type));
    }


    /// <summary>
    /// 插入一个预定记录
    /// </summary>
    /// <param name="ids">房间或者餐厅的id</param>
    /// <param name="op">订餐还是订房</param>
    private void InsertOrderList(string ids, string op)
    {

        string ordersql = @"INSERT INTO [OrderList]
           ([CustomerID]
           ,[CType]
           ,[OrderDate]
           ,[OrderName]
            ,[pcount])
     VALUES
           ('{0}'
           ,'{1}'
           ,'{2}'
           ,'{3}','{4}')";
        if (op == "DinnerTableList")
        {//订餐
            string dinnername = shujuku.GetDataTable("select areaname||diningtablename from dinnertablelist where id in (" + ids + ")").Rows[0][0].ToString();
            string date = FTchina.basic.SafeRequest(Request["date"]);//中餐还是晚餐
            string type = (date == "zhong") ? "2" : "3";//2午餐，3晚餐
            string pcount = FTchina.basic.SafeRequest(Request["pcount"]);
            string upsql = string.Format(ordersql, "009", type, basic.GetSqliteDate(), dinnername, pcount);
            shujuku.ExecuteSql(upsql);
        }
    }

    /// <summary>
    /// 得到sql语句
    /// </summary>
    /// <param name="ids"></param>
    /// <param name="tb"></param>
    /// <param name="op"></param>
    /// <returns></returns>
    protected string MakeSqls(string ids, string op,string type)
    {
        string tmp = string.Empty;
        switch (op)
        {
            case "RoomList":
                {//房间
                    if (type == "order")
                    {//预定
                        tmp = string.Format(OrderRomm, ids, op, basic.GetSqliteDate());
                    }
                    else
                    {
                        tmp = string.Format(RestRoomSql, ids, op, basic.GetSqliteDate(new DateTime(1910, 1, 1)));
                    }
                    break;
                }
            case "DinnerTableList":
                {//餐厅
                    string date = FTchina.basic.SafeRequest(Request["date"]);
                    if (type == "order")
                    {//预定
                        
                        tmp = string.Format(OrderDinner.Replace("xxxxxx",date), ids, op, basic.GetSqliteDate(),FTchina.basic.SafeRequest(Request["pcount"]));
                       // basic.Log(tmp);
                    }
                    else
                    {
                        tmp = string.Format(RestDinner.Replace("xxxxxx", date), ids, op, basic.GetSqliteDate(new DateTime(1910, 1, 1)));
                    }
                    break;
                }
            default:
                {
                    break;
                }
        }
       // LogNet.LogBLL.debug(type+tmp);
        return tmp;
    }
    /// <summary>
    /// 执行sql语句
    /// </summary>
    /// <param name="sql"></param>
    /// <returns></returns>
    protected Boolean ExecSqls(string sql)
    {
        if (sql.Length > 0)
        {
            //LogNet.LogBLL.info(sql);
            //return true;
            return shujuku.ExecuteSql(sql);
        }
        else
        {
            return false;
        }
    }

}